//
//  UpdateMobileViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/12/8.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit

class UpdateMobileViewController: UIViewController {

    @IBOutlet weak var btnReg: UIButton!
    @IBOutlet weak var btnCode: UIButton!
    @IBOutlet weak var tfCode: UITextField!
    @IBOutlet weak var tfMobile: UITextField!
    
    var isOld: Bool = true
    
    var oldMobile:String = ""
    
    var certified_status:Int = 1 // 1:no 2:yes
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "修改手机号"
        tfMobile.placeholder = "请输入旧手机号"
        self.btnReg.setTitle("确定", for: .normal)
        self.tfMobile.isEnabled = false
        self.tfMobile.text = (UIApplication.shared.delegate as! AppDelegate).user_name
        // Do any additional setup after loading the view.
    }
    
    @IBAction func getCode(_ sender: UIButton) {
        
        self.view.endEditing(true)
        
        if tfMobile.text!.count != 11 {
            showMessage(message: "请输入合法手机号")
            return
        }
        
        if isOld == true {
            NetWorkRequest(.code(user_phone: tfMobile.text!, type: "3", captcha: "1")) { (res) -> (Void) in
                if res["code"] as! String == "000000" {
                    sender.startTimer(title: "s")
                }
                showMessage(message: res["message"] as! String)
            }
        }else {
            
            if self.certified_status == 1 {
                NetWorkRequest(.code(user_phone: tfMobile.text!, type: "4", captcha: "1")) { (res) -> (Void) in
                    if res["code"] as! String == "000000" {
                        sender.startTimer(title: "s")
                    }
                    showMessage(message: res["message"] as! String)
                }
            }else {
                NetWorkRequest(.codeesp(mobile: tfMobile.text!, usage: "2")) { (res) -> (Void) in
                    if res["code"] as! String == "000000" {
                        sender.startTimer(title: "s")
                    }
                }
            }                        
        }
    }
    
    @IBAction func send(_ sender: UIButton) {
        if tfMobile.text!.count != 11 {
            showMessage(message: "请先输入正确手机号")
            return
        }
        if tfCode.text!.count != 6 {
            showMessage(message: "请先输入正确验证码位数")
            return
        }
        self.view.endEditing(true)
        showHUD()
        if isOld == true {//验证旧手机号
            NetWorkRequest(.checkCode(user_phone: tfMobile.text!, type: "3", cap: tfCode.text!)) { (res) -> (Void) in
                if res["code"] as! String == "000000" {
                    self.isOld = false
                    self.oldMobile = self.tfMobile.text!                    
                    self.tfMobile.isEnabled = true
                    self.tfMobile.text = ""
                    self.tfMobile.placeholder = "请输入新手机号"
                    self.tfCode.text = ""
                    self.btnCode.stopTimer(title: "1")
                    self.certified_status = jsonFormater(data: res["data"] as Any)["certified_status"].intValue
                }
                dismissHUD()
            }
        }else {
            NetWorkRequest(.phonemodify(p1: oldMobile, p2: tfMobile.text!, code: tfCode.text!)) { (res) -> (Void) in
                if res["code"] as! String == "000000" {
                    
                    
                    NetWorkRequest(.logout()) { (res) -> (Void) in
                    }
                    UserDefaults.User.set("", forKey: .authorization)
                    UserDefaults.User.set(false, forKey: .isLogin)
                    self.navigationController?.popToRootViewController(animated: false)
                    self.tabBarController?.selectedIndex = 0
                }
                dismissHUD()
            }
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
